package com.wanger.swa.config.filter;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author: wanger
 * @Date: 2024/10/7 1:17
 * @Description:
 */
@Slf4j
public class CustomFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) {
        // 初始化方法，可以用于加载资源或配置
        log.info("CustomFilter initialized.");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        long l = System.currentTimeMillis();
        // 继续执行后续的过滤链
        chain.doFilter(request, response);
        // 记录请求信息
        log.info("【{}】=>【{}】=>【{}】",
                httpRequest.getMethod(),
                httpRequest.getRequestURI(),
                DateUtil.formatBetween(System.currentTimeMillis() - l));
        // 后处理（可以在这里处理响应）
        //log.info("Response sent for URI: {}", httpRequest.getRequestURI());
    }

    @Override
    public void destroy() {
        // 清理资源
//        log.info("CustomFilter destroyed.");
    }
}
